/*
 * temp_plot.cpp
 *
 *  Created on: 22.07.2013
 *      Author: SpirTanol
 */

#include "ui/temp_plot.h"
#include "global.h"

// QWT
#include <qwt_scale_draw.h>
#include <qwt_legend.h>
#include <qwt_legend_item.h>
#include <qwt_plot_layout.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_picker.h>
#include <qwt_scale_widget.h>

// QT
#include <QFontMetrics>
#include <QDateTime>
//=========================================================================
TempPlot::TempPlot() : QwtPlot(), oDuration(60)
{
	init();
}

TempPlot::TempPlot(const TempPlot *plot)
{
	init();

	oTimes = plot->oTimes;
	oCurves[0].oData = plot->oCurves[0].oData;
	oCurves[1].oData = plot->oCurves[1].oData;
	oDuration = plot->oDuration;

	double dMax;
	dMax = max(oCurves[0].oData);
	if(dMax < oLeftMax)
		dMax = oLeftMax;
	setAxisScale(QwtPlot::yLeft, 0.0, dMax);

	dMax = max(oCurves[1].oData);
	if(dMax < oRightMax)
		dMax = oRightMax;
	setAxisScale(QwtPlot::yRight, 0.0, dMax);

	oCurves[0].oCurve->setData(oTimes, oCurves[0].oData);
	oCurves[1].oCurve->setData(oTimes, oCurves[1].oData);

	setAxisScale(QwtPlot::xBottom, oTimes.back() - double(oDuration), oTimes.back());

	oCurves[0].oCurve->setTitle(trUtf8("Температура: ") + QString::number(int(oCurves[0].oData.back())) + trUtf8("°C"));
	oCurves[1].oCurve->setTitle(trUtf8("Нагрев: ") + QString::number(int(oCurves[1].oData.back())) + "%");

	replot();
}

TempPlot::~TempPlot()
{

}
//=========================================================================
void TempPlot::init()
{
	oRightMax = 150.0;
	oLeftMax = 100.0;

	setAutoReplot(false);
	plotLayout()->setAlignCanvasToScales(true);
	QwtLegend *legend = new QwtLegend();
	legend->setItemMode(QwtLegend::CheckableItem);
	insertLegend(legend, QwtPlot::RightLegend);
	setCanvasBackground(QColor(240, 241, 239));
	restart();

	QwtText text(trUtf8("Температура (°C)"));
	text.setFont(QFont("Arial", 8));
	setAxisScale(QwtPlot::yLeft, 0.0, oLeftMax);
	setAxisTitle(QwtPlot::yLeft, text);

	setAxisScale(QwtPlot::yRight, 0.0, oRightMax);
	text.setText(trUtf8("Расчётный нагрев (%)"));
	setAxisTitle(QwtPlot::yRight, text);
	enableAxis(QwtPlot::yRight);

	QwtPlotCurve *curve;

	curve = new QwtPlotCurve(trUtf8("Температура"));
	curve->setPen(QColor("#FF9933"));
	curve->attach(this);
	oCurves[0].oCurve = curve;

	curve = new QwtPlotCurve(trUtf8("Расчётный нагрев"));
	curve->setPen(QColor("#FF99FF"));
	curve->attach(this);
	curve->setYAxis(QwtPlot::yRight);
	curve->setZ(curve->z() - 3);
	oCurves[1].oCurve = curve;

	showCurve(oCurves[0].oCurve, true);
	showCurve(oCurves[1].oCurve, true);

	connect(this, SIGNAL(legendChecked(QwtPlotItem *, bool)), this, SLOT(showCurve(QwtPlotItem *, bool)));

	QwtPlotGrid *grid = new QwtPlotGrid;
	grid->enableY(true);
	grid->enableYMin(true);
	grid->enableX(true);
	grid->enableXMin(false);
	grid->setMajPen(QPen(Qt::blue, 0, Qt::DotLine));
	grid->setMinPen(QPen(Qt::gray, 0 , Qt::DotLine));
	grid->attach(this);

	class Picker : public QwtPlotPicker
	{
	public:
		Picker(int x, int y, int flags, RubberBand rb, DisplayMode dm, QwtPlotCanvas *c) : QwtPlotPicker(x, y, flags, rb, dm, c)
		{
		}
		~Picker() {}

		QwtText trackerText(const QPoint &pos) const
		{
			uint time = uint(plot()->invTransform(QwtPlot::xBottom, pos.x()));
			QString str = QString::fromUtf8("Температура: ") + QString::number(int(plot()->invTransform(QwtPlot::yLeft, pos.y()))) + trUtf8("°C\n") +
					QString::fromUtf8(" Расчётный нагрев: ") + QString::number(int(plot()->invTransform(QwtPlot::yRight, pos.y()))) + trUtf8("%\n") +
					QDateTime::fromTime_t(time).time().toString();
			return str;
		}
	};

	new Picker(QwtPlot::xBottom, QwtPlot::yLeft,
	QwtPicker::PointSelection,
	QwtPicker::CrossRubberBand, QwtPicker::AlwaysOn, canvas());
}
//=========================================================================
void TempPlot::restart()
{
	class TimeScaleDraw : public QwtScaleDraw
	{
	public:
		QwtText label(double time) const // [virtual]
	    {
	    	return QDateTime::fromTime_t(uint(time)).time().toString();
	    }

	};

	setAxisScaleDraw(QwtPlot::xBottom, new TimeScaleDraw());

	oTimes.clear();
	oCurves[0].oData.clear();
	oCurves[1].oData.clear();

	setAxisLabelRotation(QwtPlot::xBottom, 0.0);
	setAxisFont(QwtPlot::xBottom, QFont("Arial", 8));
	setAxisLabelAlignment(QwtPlot::xBottom, Qt::AlignHCenter | Qt::AlignBottom);
	QwtScaleWidget *scaleWidget = axisWidget(QwtPlot::xBottom);
	const int fmh = QFontMetrics(scaleWidget->font()).height();
	scaleWidget->setMinBorderDist(0, fmh / 2);
	setAxisScale(QwtPlot::xBottom, 0.0, 100.0);
}
//=========================================================================
void TempPlot::updateData(double temp, double calcTemp)
{
	oCurves[0].oData.push_back(temp);
	oCurves[1].oData.push_back(calcTemp);

	double dMax;
	dMax = max(oCurves[0].oData);
	if(dMax < oLeftMax)
		dMax = oLeftMax;
	double dMin = min(oCurves[0].oData);
	if (dMin > 0.0)
		dMin = 0.0;
	setAxisScale(QwtPlot::yLeft, dMin, dMax);

	dMax = max(oCurves[1].oData);
	if(dMax < oRightMax)
		dMax = oRightMax;
	setAxisScale(QwtPlot::yRight, 0.0, dMax);

	oTimes.push_back(QDateTime::currentDateTime().toTime_t());

	while (uint(oTimes.back() - oTimes.front()) > oDuration + 5)
	{
		oTimes.pop_front();
		oCurves[0].oData.pop_front();
		oCurves[1].oData.pop_front();
	}

	oCurves[0].oCurve->setData(oTimes, oCurves[0].oData);
	oCurves[1].oCurve->setData(oTimes, oCurves[1].oData);

	setAxisScale(QwtPlot::xBottom, oTimes.back() - double(oDuration), oTimes.back());

	oCurves[0].oCurve->setTitle(trUtf8("Температура: ") + QString::number(int(oCurves[0].oData.back())) + trUtf8("°C"));
	oCurves[1].oCurve->setTitle(trUtf8("Нагрев: ") + QString::number(int(calcTemp)) + "%");

	replot();
}
//=========================================================================
void TempPlot::showCurve(QwtPlotItem *item, bool on)
{
	item->setVisible(on);
	QWidget *w = legend()->find(item);
	if ( w && w->inherits("QwtLegendItem") )
		((QwtLegendItem *)w)->setChecked(on);
	replot();
}
//=========================================================================
